package com.qumap.map;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.qumap.common.event.GlobalEvent;
import com.qumap.common.event.GlobalEventCollection;
import com.qumap.common.event.GlobalEventHandler;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 06.05.11
 * Time: 15:34
 * To change this template use File | Settings | File Templates.
 */
public class Map extends MapItemHolder {

    private final PopupPanel popup = new PopupPanel(false,true);
    private Image inactive_image;

    @Override
    public void init() {
        initDeactivateWidget();
        super.init();
    }

    private void initDeactivateWidget() {
        inactive_image = new Image(map_view.getInactiveStateImageURL());
        popup.setWidget(inactive_image);
    }

    @Override
    protected void initListeners() {
        super.initListeners();    //To change body of overridden methods use File | Settings | File Templates.
        initGlobalEventListeners();
    }

    private void initGlobalEventListeners() {

        global_event_bus.addHandler(GlobalEventCollection.EventType.Activate.getType(), new GlobalEventHandler() {
            @Override
            public void onGlobalEvent(GlobalEvent event) {
                onActivate();
            }
        });

        global_event_bus.addHandler(GlobalEventCollection.EventType.Deactivate.getType(), new GlobalEventHandler() {
            @Override
            public void onGlobalEvent(GlobalEvent event) {
                onDeactivate();
            }
        });


    }

    protected void onActivate() {
        popup.hide();
    }

    protected void onDeactivate() {
        int position_x = (map_view.asWidget().getOffsetWidth()- inactive_image.getWidth())/2+map_view.asWidget().getAbsoluteLeft();
        int position_y = (map_view.asWidget().getOffsetHeight()- inactive_image.getHeight())/2+map_view.asWidget().getAbsoluteTop();
        popup.setPopupPosition(position_x, position_y);
        popup.show();
    }


}


